package btb.engine;

import btb.engine.CEvent;
import btb.engine.CEventList;
import btb.util.AObjectMap;

@SuppressWarnings("unused")
public class CTaskMessenger extends AObjectMap
{	
//	private int displayThread;
//	private int inputThread;
//	private int clientThread;
//	private int serverThread;
	
	public static final int TASK_MAIN	 	= 0;
	public static final int TASK_DISPLAY 	= 1;
	public static final int TASK_INPUT		= 2;
	public static final int TASK_MCLIENT	= 10;
	public static final int TASK_GCLIENT	= 11;
	public static final int TASK_CCLIENT	= 12;
	public static final int TASK_MSERVER	= 20;
	public static final int TASK_GSERVER	= 21;
	public static final int TASK_CSERVER	= 22;
	
	public int push(int _threadId, CEvent _event)
	{
		int pos = findKey(_threadId);
		
		if(pos > -1)
		{
			try
			{
				CEventList cel = (CEventList) this.list[pos];
				cel.add(_event);
			}
			catch(Exception e)
			{
				return -1;
			}
		}
		else
		{
			CEventList cel = new CEventList();
			cel.add(_event);
			this.add(cel, _threadId);
		}
		return 0;
	}
	
	public CEvent pull(int _threadId)
	{
		int pos = findKey(_threadId);
		CEvent event;
		
		//System.out.println("pos: " + pos);
		
		if(pos > -1)
		{
			try{
				CEventList mailbox = (CEventList) this.list[pos];
				event = (CEvent) mailbox.getList()[0];
				mailbox.removeByIndex(0);
			}
			catch(Exception e)
			{
				return null;
			}
			return event;
		}
		return null;
	}
	
//	public void setClientVars(int _display, int _input, int _network)
//	{
//		if(_display > 0)
//		{
//			this.displayThread = _display;
//		}
//		if(_input > 0)
//		{
//			this.inputThread = _input;
//		}
//		if(_network > 0)
//		{
//			this.clientThread = _network;
//		}
//	}
//	
//	public void setServerVars(int _server) {
//		if(_server > 0)
//			this.serverThread = _server;
//	}
//	
//	public int getDisplayThreadId()
//	{
//		return this.displayThread;
//	}
//	
//	public int getInputThreadId()
//	{
//		return this.inputThread;
//	}
//	
//	public int getNetworkThreadId()
//	{
//		return this.clientThread;
//	}
	
	public int getEventCount(int _threadId)
	{
		int pos = findKey(_threadId);
		
		if(pos > -1)
		{
			try{
				return ((CEventList) this.list[pos]).getCount();
			}
			catch(Exception e)
			{
				return -1;
			}
		}
		return -2;
	}
	
	public int getTotalEventCount()
	{
		int total = 0;
		
		try
		{
			for(int i = 0; i < this.getCount(); i++)
				total += ((CEventList)this.list[i]).getCount();
		}
		catch(Exception e)
		{
			return -1;
		}
		return total;
	}
}
